// pages/person/person.js
const app = getApp();
const api = require('../../config/api.js');
const myRequest = require('../../config/myRequest.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    noUserInfo: true,
    goodsname: '',
    switchStatus: false,
    seller_name: ''
  },
  toLogin() {
    app.globalData.token = null;
    wx.setStorage({
      key: 'acount',
      data: ''
    })
    wx.setStorage({
      key: 'password',
      data: ''
    })
    wx.setStorage({
      key: 'ischeck',
      data: false
    })
    wx.reLaunch({
      url: '/pages/login/login'
    })
  },

  // 商品名称
  changeName(e) {
    const that = this;
    that.setData({
      goodsname: e.detail.value
    })
  },
  listenerSwitch(e) {
    const that = this;
    that.setData({
      hiddenLoading: false
    })
    const json = {
      token: app.globalData.token
    }
    myRequest.EasyHttp.post(api.reqUrls.usersetMsgOpen, json).then(res => {
      console.log(res)
      that.setData({
        hiddenLoading: true
      })
      if (res.data.status == 1) {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
        })
        that.setData({
          switchStatus: ! that.data.switchStatus,
        })
      }else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
        })
      }
    })
  },
  qYWXAccount(){
    const that = this;
    if(!that.data.goodsname){
      wx.showToast({
        title: '请填写企业微信账号',
        icon: 'none',
      })
      return
    }
    that.setData({
      hiddenLoading: false
    })
    const json = {
      token: app.globalData.token,
      qiyewx_account: that.data.goodsname,
    }
    myRequest.EasyHttp.post(api.reqUrls.setQYWXAccount, json).then(res => {
      console.log(res)
      that.setData({
        hiddenLoading: true
      })
      if (res.data.status == 1) {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
        })
      }else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this;
    if (app.globalData.token) {
      that.setData({
        hiddenLoading: false
      })
      const json = {
        token: app.globalData.token
      }
      myRequest.EasyHttp.post(api.reqUrls.userInfo, json).then(res => {
        console.log(res)
        that.setData({
          hiddenLoading: true
        })
        if (res.data.status == 1) {
          that.setData({
            noUserInfo: true,
            userInfo: res.data.result.seller_info,
            goodsname: res.data.result.seller_info.qiyewx_account,
            switchStatus: res.data.result.seller_info.open_send_qiyewx,
            seller_name: res.data.result.seller_info.seller_name,
          })
        }else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }
      })
    } else {
      that.setData({
        noUserInfo: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})